#include "linearCombination.h"
#include <QImage>
#include <iostream>

LinearCombination::LinearCombination(float alpha, QImage *original, QImage* watermark)
        : Watermark(original, watermark), alpha (alpha)
{
}

QImage* LinearCombination::applyWatermark() {
    //if(watermarkedImage)
    //    delete watermarkedImage;
    std::cout << "apply watermark" << std::endl;
    if (!mainImage)
        std::cout << "fudeu mainImage eh null" << std::endl;
    watermarkedImage = new QImage(mainImage->width(), mainImage->height(), QImage::Format_RGB888);
    //Supomos que as 2 imagens tem as mesmas dimensões.
    std::cout << "width: " << mainImage->width() << std::endl;
    std::cout << "height: " << mainImage->height() << std::endl;

    for(int i = 0; i < mainImage->width(); i++)
        for(int j = 0; j < mainImage->height(); j++) {
            QRgb orig = mainImage->pixel(i ,j);
            int origR = qRed(orig);
            int origG = qGreen(orig);
            int origB = qBlue(orig);

            QRgb wm = watermark->pixel(i, j);
            int wmR = qRed(wm);
            int wmG = qRed(wm);
            int wmB = qRed(wm);
            
            int watermarkedR = (1 - alpha)*origR + alpha*wmR;
            int watermarkedG = (1 - alpha)*origG + alpha*wmG;
            int watermarkedB = (1 - alpha)*origB + alpha*wmB;

            watermarkedImage->setPixel(i, j, qRgb(watermarkedR, watermarkedG, watermarkedB));
        }

    return watermarkedImage;
}
